#include "AASquare.h"
#include "Ray.h"

AASquare::AASquare(float w, Vector3 c, int axis)
: m_width(w), m_center(c)
{
	m_min = Vector3(m_center - m_width*0.5);
	m_max = Vector3(m_center + m_width*0.5);
	m_min[axis] = m_center[axis];
	m_max[axis] = m_center[axis];
	m_axis = axis;
}

AASquare::~AASquare()
{
}

bool AASquare::intersect(HitInfo& result, const Ray& ray)
{	
	Vector3 inv_d = Vector3(1/ray.d.x, 1/ray.d.y, 1/ray.d.z);
	float t = (m_center[m_axis] - ray.o[m_axis])*inv_d[m_axis];

	int u = (m_axis + 1) % 3;
	int v = (m_axis + 2) % 3;

	Vector3 p = ray.o + t*ray.d;
	if(t>0. && (p[u] > m_min[u]) && (p[v] > m_min[v]) && (p[u] < m_max[u]) && (p[v] < m_max[v])) {
		result.t = t;
		result.P = p;
		if(m_axis == 1) // light
		{
			result.material = 1;
			result.N = Vector3(0,-1,0);
		}
		else // mirror
		{
			result.material = 2;
			result.N = Vector3(-1,0,0);
		}
		return true;
	}

	return false;
}
